﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Diagnostics;
using System.Data.Linq.Mapping;
using System.Runtime.Serialization;

using SharedGenomics.Core;

namespace SharedGenomics.Domain
{
    [DataContract]
    public class GenderFileSchemaColumn : NominalFileSchemaColumn
    {
        public GenderFileSchemaColumn()
            : base(ColumnDistribution.Gender)
        {
            //this.Initialise();
        }

        internal GenderFileSchemaColumn(string maleValue, string femaleValue)
            : this()
        {
            maleValue.ThrowIfNull("maleValue");
            femaleValue.ThrowIfNull("femaleValue");

            this.AddValue(maleValue, "Male", false);
            this.AddValue(femaleValue, "Female", false);

            this.MaleValue = maleValue.Trim();
            this.FemaleValue = femaleValue.Trim();
        }

        public override object Parse(string value)
        {
            if (value.IsEmptyOrWhitespace())
                return Sex.Unknown;
            else
            {
                Debug.Assert(value != null);
                string trimmedValue = value.Trim();

                if (trimmedValue == this.MaleValue)
                    return Sex.Male;
                else if (trimmedValue == this.FemaleValue)
                    return Sex.Female;
                else
                    return Sex.Unknown;
            }
        }

        [OnDeserializing]
        internal void OnDeserialising(StreamingContext context)
        {
            this.Initialize();
        }

        [Column(DbType = "varchar(32)")]
        [DataMember]
        public string MaleValue { get; set; }

        [Column(DbType = "varchar(32)")]
        [DataMember]
        public string FemaleValue { get; set; }

        public override bool IsValidPhenotypeColumn
        {
            get { return false; }
        }
    }
}
